﻿using Abp.Application.Services.Dto;
using DevExpress.XtraLayout.Utils;
using Sw.Hospital.HealthExaminationSystem.ApiProxy.OccdiseaseSet.OccdiseaseMain;
using Sw.Hospital.HealthExaminationSystem.Application.Charge.Dto;
using Sw.Hospital.HealthExaminationSystem.Application.InspectionTotal;
using Sw.Hospital.HealthExaminationSystem.Application.InspectionTotal.Dto;
using Sw.Hospital.HealthExaminationSystem.Application.OccdiseaseSet.OccdiseaseMain;
using Sw.Hospital.HealthExaminationSystem.Application.OccdiseaseSet.OccdiseaseMain.Dto;
using Sw.Hospital.HealthExaminationSystem.Application.OccDisProposalNew;
using Sw.Hospital.HealthExaminationSystem.Application.OccDisProposalNew.Dto;
using Sw.Hospital.HealthExaminationSystem.Common.Bases;
using Sw.Hospital.HealthExaminationSystem.Common.Enums;
using Sw.Hospital.HealthExaminationSystem.Common.UserCache;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Sw.Hospital.HealthExaminationSystem.InspectionTotal
{
    public partial class frmCusRiskSum : UserBaseForm
    {
        private TjlCustomerRegForInspectionTotalDto _tjlCustomerRegDto;
        public OccCustomerHazardSumDto _cusHazar;
        // 总检
        private readonly IInspectionTotalAppService _inspectionTotalService;
        public readonly IOccDisProposalNewAppService _IOccDisProposalNewAppService;
        private readonly string _riskName;
        private readonly IOccDiseaseAppService _OccDiseaseAppService;
        private readonly IOccDisProposalNewAppService _OccDisProposalNewAppService;
        public frmCusRiskSum()
        {
            InitializeComponent();
            _inspectionTotalService = new InspectionTotalAppService();
            _IOccDisProposalNewAppService = new OccDisProposalNewAppService();
            _OccDiseaseAppService = new OccDiseaseAppService();
            _OccDisProposalNewAppService = new OccDisProposalNewAppService();
        }
        public frmCusRiskSum(TjlCustomerRegForInspectionTotalDto custreg, OccCustomerHazardSumDto cusHazar, string riskName) : this()
        {
            _tjlCustomerRegDto = custreg;
            _cusHazar = cusHazar;
            _riskName = riskName;

        }

        private void frmCusRiskSum_Load(object sender, EventArgs e)
        {
            string strwjshow = DefinedCacheHelper.GetBasicDictionaryByValue(BasicDictionaryType.CusSumSet, 17)?.Remarks;
            if (!string.IsNullOrEmpty(strwjshow) && strwjshow == "Y")
            {
                //layoutOccContent.Visibility = LayoutVisibility.Never;
                layoutMedicalAdvice.Visibility = LayoutVisibility.Never;
            }

            //职业病
            var clientcontract = Enum.GetName(typeof(BasicDictionaryType), BasicDictionaryType.ExaminationType);
            var Clientcontract = DefinedCacheHelper.GetBasicDictionary().Where(o => o.Type == clientcontract);
            var tjlb = Clientcontract.FirstOrDefault(o => o.Value == _tjlCustomerRegDto.PhysicalType)?.Text;

            labZYB.Text = string.Format("体检类型：{0} 危害因素：{1} 岗位： {2} 工种：{3}", _tjlCustomerRegDto.PostState,
                   _riskName, _tjlCustomerRegDto.WorkName, _tjlCustomerRegDto.TypeWork);
            EntityDto<Guid> entityDto = new EntityDto<Guid>();
            entityDto.Id = _tjlCustomerRegDto.Id;
            var Occ = _inspectionTotalService.GetOccSumByCusReg(entityDto);
            var Issy = DefinedCacheHelper.GetBasicDictionaryByValue(BasicDictionaryType.CusSumSet, 121)?.Remarks;

            if (tjlb.Contains("放射") || (!string.IsNullOrEmpty(Issy) && Issy == "1"))
            {

               

                var OccDiseaseDtos = new Occdieaserucan();
                OccDiseaseDtos.IsActive = 1;
                var ConDictionary = _OccDiseaseAppService.GetAllOccDisease(OccDiseaseDtos);

                SearOccDis.Properties.DataSource = ConDictionary;
                SearOccDis.Properties.DisplayMember = "Text";
                SearOccDis.Properties.ValueMember = "Id";

                var showOccDictionary = new ShowOccDictionary();
                showOccDictionary.Type = "Contraindication";
                showOccDictionary.IsActive = 3;
                var result = _OccDisProposalNewAppService.GetAll(showOccDictionary);

                searJJZ.Properties.DataSource = result;
                searJJZ.Properties.DisplayMember = "Text";
                searJJZ.Properties.ValueMember = "Id";
            }
            else
            {
                SearOccDis.Properties.DataSource = Occ.OccDiseases.Where(p => p.OccHazardFactorsId == _cusHazar.OccHazardFactorsId).ToList();
                SearOccDis.Properties.DisplayMember = "Text";
                SearOccDis.Properties.ValueMember = "Id";
             
                searJJZ.Properties.DataSource = Occ.Contraindications.Where(p => p.OccHazardFactorsId == _cusHazar.OccHazardFactorsId).ToList();
                searJJZ.Properties.DisplayMember = "Text";
                searJJZ.Properties.ValueMember = "Id";
            }
            //处理意见
            ChargeBM chargeBM = new ChargeBM();
            chargeBM.Name = ZYBBasicDictionaryType.Opinions.ToString();
            var lis1 = _IOccDisProposalNewAppService.getOutOccDictionaryDto(chargeBM);

            if (tjlb.Contains("放射"))
            {
                lis1 = lis1.Where(p => p.Remarks.Contains("放射")).ToList();
            }
            else
            {
                lis1 = lis1.Where(p => !p.Remarks.Contains("放射")).ToList();
            }

            //searOccAdvice.Properties.DataSource = lis1;
            searOccAdvice.Items.Clear();
            foreach (var yj in lis1)
            {
                searOccAdvice.Items.Add(yj.Text);
            }
            //职业病结论

            chargeBM.Name = ZYBBasicDictionaryType.Conclusion.ToString();
            var lis2 = _IOccDisProposalNewAppService.getOutOccDictionaryDto(chargeBM);
            txtZYBSum.Properties.DataSource = lis2;
       
            if (_cusHazar != null)
            {
                txtZYBSum.EditValue = _cusHazar.Conclusion;
                searOccAdvice.Text = _cusHazar.Advise;
                txtOccContent.EditValue = _cusHazar.Description;
                txtMedicalAdvice.EditValue = _cusHazar.MedicalAdvice;
                SearOccDis.EditValue = _cusHazar.OccCustomerOccDiseasesIds;
                SearOccDis.RefreshEditValue();
                searJJZ.EditValue = _cusHazar.OccDictionarysIDs;
                searJJZ.RefreshEditValue();
            }
        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            if (_cusHazar == null)
            {
                _cusHazar = new OccCustomerHazardSumDto();
            }
            _cusHazar.Conclusion = txtZYBSum.EditValue?.ToString();
            _cusHazar.Advise= searOccAdvice.Text ;
            _cusHazar.Description= txtOccContent.EditValue?.ToString();
            _cusHazar.MedicalAdvice= txtMedicalAdvice.EditValue?.ToString();
            _cusHazar.OccCustomerOccDiseasesIds= SearOccDis.EditValue?.ToString() ;
            _cusHazar.OccDictionarysIDs = searJJZ.EditValue?.ToString();

            this.DialogResult = DialogResult.OK;
            this.Close();
            
        }

        private void simpleButton2_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
